package com.grandtech.insurance.common.utils;


import com.grandtech.insurance.common.utils.trans.Transformation;
import com.mapbox.geojson.Geometry;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class StringUtil {

    public static String getUUID(){
        return  java.util.UUID.randomUUID().toString().replace("-","");
    }


    /**
     * 保留小数
     * @param d
     * @param r 保留位数
     * @return
     */
    public static String round(Double d,int r){
        if (d==null){
            d=0d;
        }
        String f ="#.";
        for(int i =0;i<r;i++){
            f+="0";
        }
        DecimalFormat df = new DecimalFormat(f);
        return df.format(d);
    }


    /**
     * 截取字符串str中指定字符 strStart、strEnd之间的字符串
     *
     * @param str
     * @param strStart
     * @param strEnd
     * @return
     */
    public static String subString(String str, String strStart, String strEnd) {

        /* 找出指定的2个字符在 该字符串里面的 位置 */
        int strStartIndex = str.indexOf(strStart);
        int strEndIndex = str.indexOf(strEnd);

        /* index 为负数 即表示该字符串中 没有该字符 */
        if (strStartIndex < 0) {
            return null;
        }
        if (strEndIndex < 0) {
            return null;
        }
        /* 开始截取 */
        String result = str.substring(strStartIndex, strEndIndex).substring(strStart.length());
        return result;
    }


    /**
     * 返回坐标串
     * @param codes
     * @return
     */
    public static  String  getCodes(String codes) {
        codes = StringUtil.subString(codes, "((", "))");
        if (codes != null) {
            codes = codes.replace("  ", " ");
            codes = codes.replace(" ", "|");
            String c = "";
            for (String p : codes.split(",")) {
                if (p.contains("|")) {
                    for (String d : p.split("\\|")) {
                        if (!d.equals("") && !d.contains(" ")) {
                            c += StringUtil.round(Double.valueOf(d), 6) + "|";
                        }
                    }
                    if (c.length() > 1) {
                        c = c.substring(0, c.length() - 1) + ",";
                    }
                }
            }
            if (c.length() > 1) {
                c = c.substring(0, c.length() - 1);
            }
            c = c.replace("|", " ");
            return c;
        }
        return "";
    }


    /**
     * 返回坐标串（POLYGON一条线的）
     * @param  geometry
     * @return
     */
    public static  String  getCodesByGeometry(Geometry geometry) {
        if(geometry==null){
            return "";
        }
        try {
            System.out.println("处理前geometry-------"+  Transformation.boxGeometry2Wkt(geometry));
            String codes_r = "";
            String codes = Transformation.boxGeometry2Wkt(geometry);
            Double area = Transformation.compute84GeoJsonArea(geometry.toJson());
            List<String> codeList = new ArrayList<>();
            codes = StringUtil.subString(codes, "((", "))");
            if (codes != null) {
                codes = codes.replace("  ", " ");
                codes = codes.replace(" ", "|");
                for (String p : codes.split(",")) {
                    if (p.contains("|")) {
                        String c = "";
                        for (String d : p.split("\\|")) {
                            if (!d.equals("") && !d.contains(" ")) {
                                c += StringUtil.round(Double.valueOf(d), 6) + ",";
                            }
                        }
                        if (c.length() > 1) {
                            c = c.substring(0, c.length() - 1) ;
                            codeList.add(c);
                        }
                    }
                }
                if(codeList.size()>0){
                    if(codeList.get(0).equals(codeList.get(codeList.size()-1))){
                        codeList.remove(codeList.size()-1);
                    }
                }
                if(area>0){
                    for(int i=codeList.size()-1 ;i>=0;i--){
                        codes_r+=codeList.get(i)+";";
                    }
                }else {
                    for(int i=0 ;i<codeList.size();i++){
                        codes_r+=codeList.get(i)+";";
                    }
                }
                System.out.println("处理后坐标------"+area+"---------" + codes_r);
                return codes_r;
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("处理错误------"+Transformation.boxGeometry2Wkt(geometry));
            return  Transformation.boxGeometry2Wkt(geometry);
        }
        return "";
    }

    /**
     * geometry返回坐标串(支持POLYGON单端，多段，MULTIPOLYGON)
     * @param  geometry
     * @return
     */
    public static  String  getCodesByGeometryV1(Geometry geometry) {
        if(geometry==null){
            return "";
        }
        String wkt = Transformation.boxGeometry2Wkt(geometry);
        return wkt2Codes(wkt);
    }

    /**
     * wkt返回坐标串(支持POLYGON单端，多段，MULTIPOLYGON)
     * @param  codes
     * @return
     */
    public static String wkt2Codes(String codes){
        try {
            System.out.println("处理前geometry-------"+  codes);
            String codes_r = "";
            String[] codesarr;
            /*********** 将坐标串通用化格式处理都变成(111 222,333 444),(666 77,66 44)**************************/
            if(codes.startsWith("POLYGON")){
                codes = codes.replace("POLYGON(","");
                codes = codes.replace("POLYGON (","");
                codes = codes.substring(0,codes.length()-1);
            }
            if(codes.startsWith("MULTIPOLYGON")){
                codes = codes.replace("MULTIPOLYGON((","");
                codes = codes.replace("MULTIPOLYGON ((","");
                codes = codes.substring(0,codes.length()-2);
                codes = codes.replace(")),((","),(");
            }
            /***********(111 222,333 444),(666 77,66 44)格式数据转化为数组codesarr[(111,222;333,444),(666,77;66,44)]**************************/
            codes = codes.replace("),(",")|(");
            codes = codes.replace("), (",")|(");
            codes = codes.replace(", ",";");
            codes = codes.replace(",",";");
            codes = codes.replace(" ",",");
            codesarr = codes.split("\\|");
            /***********循环将每个坐标xy取出来保留六位**************************/
            for(int i =0 ;i<codesarr.length;i++){
                String c = codesarr[i];
                /***********转换成111,222;333,444**************************/
                c = c.replace("(","").replace(")","");
                String cA="";
                String[] carr = c.split(";");
                for(int j =0 ;j<carr.length;j++){
                    String s = carr[j];
                    String[] sarr = s.split(",");
                    cA = cA + StringUtil.round(Double.valueOf(sarr[0]), 6)+",";
                    cA = cA + StringUtil.round(Double.valueOf(sarr[1]), 6)+";";
                }
                cA = cA.substring(0,cA.length()-1);
                codes_r = codes_r+"("+cA+"),";
            }
            codes_r = codes_r.substring(0,codes_r.length()-1);
            if(codesarr.length==1){
                codes_r = codes_r.replace("(","").replace(")","");
            }
            return codes_r;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("处理错误------"+codes);
            return  codes;
        }
    }
}
